package com.jwthhealth_pub.old.acupressure.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.acupressure.module.AcupressureJlpicInfoModule;
import com.jwthhealth_pub.old.acupressure.module.AcupressureJlpicModule;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.common.ImageLoader;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/12.
 */
public class MeridianInfoActivity extends Activity {
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.meridian_name)
    TextView meridianName;
    @BindView(R.id.name_layout)
    RelativeLayout nameLayout;
    @BindView(R.id.index_left)
    RelativeLayout indexLeft;
    @BindView(R.id.index_right)
    RelativeLayout indexRight;
    @BindView(R.id.item_location)
    TextView itemLocation;
    @BindView(R.id.item_location_describe)
    TextView itemLocationDescribe;
    @BindView(R.id.item_treat)
    TextView itemTreat;
    @BindView(R.id.item_treat_describe)
    TextView itemTreatDescribe;
    @BindView(R.id.meridian_index)
    TextView meridianIndex;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;

    private DisplayImageOptions thumbOptions;
    private ImageLoader imageLoader;
    private AcupressureJlpicModule.DataBean data;
    private AcupressureJlpicInfoModule.DataBean localData;
    private int index = 0;
    private boolean isReady;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meridianinfo);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        data = (AcupressureJlpicModule.DataBean) getIntent().getExtras().getSerializable(Constant.ACUPRESSUREDATA);

        initTop();

        Call<AcupressureJlpicInfoModule> acupressureJlpicModuleCall = ApiHelper.acupressureInfo(data.getJid());
        acupressureJlpicModuleCall.enqueue(new Callback<AcupressureJlpicInfoModule>() {

            private AcupressureJlpicInfoModule body;

            @Override
            public void onResponse(Call<AcupressureJlpicInfoModule> call, Response<AcupressureJlpicInfoModule> response) {
                body = response.body();
                if (body.getCode().equals("0")) {
                    localData = body.getData();
                    handler.sendEmptyMessage(0);
                } else {
                    Log.d("MeridianInfoActivity", "fail");
                }
            }

            @Override
            public void onFailure(Call<AcupressureJlpicInfoModule> call, Throwable t) {
                Log.d("MeridianInfoActivity", "error");
            }
        });
    }

    private void initTop() {
        signTopbar.setTitle("穴位保健 " + data.getName());
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                progressbar.setVisibility(View.GONE);
                final String url = localData.getThumb();
                ImageLoader.picasso(url,img);
//                Picasso.with(MeridianInfoActivity.this).load(url).into(img);
                isReady = true;
                initData();
                img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(MeridianInfoActivity.this, MeridianImgActivity.class);

                        intent.putExtra(Constant.ACUPRESSUREDATA, url);
                        startActivity(intent);
                    }
                });
            }
        }
    };

    private void initData() {
        AcupressureJlpicInfoModule.DataBean.ListBean listBean = localData.getList().get(index);
        meridianName.setText(listBean.getName());
        itemLocationDescribe.setText(listBean.getDesc());
        itemTreatDescribe.setText(listBean.getMain_desc());
        meridianIndex.setText(index + 1 + "/" + localData.getList().size());
    }

    @OnClick({R.id.index_left, R.id.index_right})
    public void onClick(View view) {
        if (!isReady) {
            return;
        }
        switch (view.getId()) {
            case R.id.index_left:
                if (!(index <= 0)) {
                    index--;
                    initData();
                }
                break;
            case R.id.index_right:
                if (index < localData.getList().size() - 1) {
                    index++;
                    initData();
                }
                break;
        }
    }
}
